﻿using Esosi.Register.Base;
using Esosi.Register.Base.Dtos;
using Esosi.Register.Hospital;
using Microsoft.Extensions.DependencyInjection;
using System;
using AutoMapper;
using Esosi.Repository;
using Esosi.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using System.Configuration;
using Esosi.Repository.Models;

namespace Esosi.Register.Test
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var tpid = new RegisterCalcTpiDto
            {
                PatientInfoDto = new PatientInfoDto
                {
                    PatientName = "Sss",
                    PatientSubTypeId = 1000000081
                },
                BalanceSource = BalanceSource.Person,
                DeptId = 1000000808,
                ClinicType = ClinicType.Normal,
                RegisterType = RegisterType.Normal
            };

            try
            {
                var sp = GetRegisterCalc(tpid.BalanceSource);
                var registerHospital = sp.GetService<IRegisterCalc>();
                var tpod = registerHospital.RegisterCalc(tpid);
                Console.WriteLine(tpod.MustPay);
                string IsBalance = string.Empty;
                //Console.WriteLine("是否结算(1或0):");
                // IsBalance = Console.ReadLine();
                IsBalance = "1";
                if (IsBalance == "0") return;
                var registerBlanceHospital = sp.GetService<IRegisterBalance>();

                var SaveResult = registerBlanceHospital.RegisterBalance(tpod.JsonGuid);
                if (SaveResult.SaveResult)
                {
                    Console.WriteLine("保存成功!");
                }
                registerBlanceHospital.SendThird += SendYiLian;
                registerBlanceHospital.SendThird += SendTuJu;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void SendYiLian(RegisterBalanceTpoDto registerBalanceTpoDto)
        {
            Console.WriteLine("YiLian");
        }

        private static void SendTuJu(RegisterBalanceTpoDto registerBalanceTpoDto)
        {
            Console.WriteLine("TuJu");
        }

        private static ServiceProvider GetRegisterCalc(BalanceSource balanceSource)
        {
            //     var builder = new ConfigurationBuilder().set.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", true, true);

            var Configuration = builder.Build();
            var connString = Configuration.GetConnectionString("HisConnectString");

            var serviceCollection = new ServiceCollection()
             .AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies())
             .AddSingleton<IRegisterCalcRespository, RegisterCalcRespository>()
             .AddSingleton<IRegisterBalanceRespository, RegisterBalanceRespository>()
            .AddDbContext<HisContext>(option =>
            {
                option.UseOracle(connString, b => b.UseOracleSQLCompatibility("11"));
            })
            ;

            switch (balanceSource)
            {
                case BalanceSource.Person:
                    serviceCollection.AddSingleton<IRegisterCalc, RegisterCalcHospital>();
                    //serviceCollection.AddSingleton<IRegisterCalcRespository, RegisterCalcRespository>();
                    serviceCollection.AddSingleton<IRegisterBalance, RegisterBalanceHospital>();
                    break;

                case BalanceSource.ShangHaiMedical:
                    break;

                default:
                    break;
            }

            return serviceCollection.BuildServiceProvider();
            //var registerHospital = serviceProvider.GetService<IRegisterCalc>();
            //return registerHospital;
        }
    }
}